package com.example.smartcitymovie.ui.selfcenter;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.navigation.Navigation;
import androidx.room.Room;

import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;

import com.example.smartcitymovie.MyApplication;
import com.example.smartcitymovie.R;
import com.example.smartcitymovie.bean.CHangePasswordBean;
import com.example.smartcitymovie.bean.ResultBean;
import com.example.smartcitymovie.dao.AppDatabase;
import com.example.smartcitymovie.dao.Userdao;
import com.example.smartcitymovie.databinding.ActivityChangePasswordBinding;
import com.example.smartcitymovie.utils.OkHttpUtils;

import java.io.IOException;
import java.util.Objects;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class ChangePasswordActivity extends AppCompatActivity {

    ActivityChangePasswordBinding binding;
    Userdao userdao;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityChangePasswordBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        AppDatabase db = Room.databaseBuilder(ChangePasswordActivity.this,
                AppDatabase.class, "database").build();
        userdao = db.userDao();
        getSupportActionBar().setTitle("修改密码");
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        initView();
    }

    private void initView() {
        binding.btnChangePassword.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                CHangePasswordBean cHangePasswordBean = new CHangePasswordBean();
                cHangePasswordBean.setOldPassword(Objects.requireNonNull(binding.TIETOldPassword.getText()).toString());
                cHangePasswordBean.setNewPassword(Objects.requireNonNull(binding.TIETNewPassword.getText()).toString());
                String passwordJson = MyApplication.getGson().toJson(cHangePasswordBean);

                int userId = MyApplication.getSharedPreferences().getInt("userId", 0);
                new Thread(() -> {
                    int rows = userdao.resetPassword(cHangePasswordBean.getOldPassword(), cHangePasswordBean.getNewPassword(), userId);
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            if (rows > 0){
                                Toast.makeText(ChangePasswordActivity.this, "修改成功", Toast.LENGTH_SHORT).show();
                                MyApplication.getSharedPreferencesEditor().clear().apply();
                                ChangePasswordActivity.this.finish();
                            }else{
                                Toast.makeText(ChangePasswordActivity.this,"原密码错误", Toast.LENGTH_SHORT).show();
                            }
                        }
                    });
                }).start();

            }
        });
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()){
            case android.R.id.home:
                this.finish();
                break;
        }
        return super.onOptionsItemSelected(item);
    }
}